package com.sc.site.member;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.member.Captcha;
import com.sc.dal.model.member.Member;
import com.sc.service.member.CaptchaService;
import com.sc.service.member.MemberService;
import com.sc.util.data.Common;
import com.sc.util.data.DataUtil;
import com.sc.util.data.SessionContainer;
import com.sc.util.exception.ExceptionEnum;
@Controller
@RequestMapping(value="/register")
public class RegisterController {
	
	/**引入用户业务层*/
	@Autowired
	private MemberService memberService;
	/**引入验证码的业务层*/
	@Autowired
	private CaptchaService captchaService;		
	@Autowired
	private EhCacheCacheManager factory;
	
	
	/**跳转到找回密码页面*/
	@RequestMapping("/findPwdPre")
	public String findPwdPre(){
		return "/member/forgotPassword";
	}
	/**跳转到注册页面*/
	@RequestMapping("/registerPre")
	public String registerPre(){
		return "/member/register";
	}
	/**跳转到注册页面*/
	@RequestMapping("/password")
	public String password(){
		return "/member/register";
	}
	/**跳转到注册输入密码页面*/
	@RequestMapping("/registerBetween")
	public String registerBetween(String mPhone,HttpServletRequest request){
		request.setAttribute("mPhone", mPhone);
		return "/member/reg_set_psw";
	}
	/**跳转到找回密码页面*/
	@RequestMapping(value="/find_paw")
	public String find_paw(){
		return "/member/find_paw";
	}
	/**验证图片验证码和用户信息*/
	@RequestMapping(value="/validateImgCode")
	@ResponseBody
	public Map<String, Object> validateImgCode( HttpServletRequest request,String mPhone,String imgCode){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			imgCode=imgCode.toUpperCase();
			Member member=null;
			String code=(String) request.getSession().getAttribute("code");
			if(imgCode.equals(code)){
				member=memberService.selectByPhone(mPhone);
				if(member==null){
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_NOT_REGISTER);
					return resultMap;
				}else{
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, mPhone);
					return resultMap;
				}
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_CAPTCHA_ERROR);
				return resultMap;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_CAPTCHA_ERROR);
			return resultMap;
		}
		
	}
	/**跳转到身份验证页面*/
	@RequestMapping(value="/verify_id")
	public String verify_id(HttpServletRequest request,String mPhone){
		request.setAttribute("mPhone", mPhone);
		return "/member/verify_id";
	}
	
	
	/**验证手机验证码*/
	@RequestMapping(value="/validateCode")
	@ResponseBody
	public Map<String, Object> validateCode(String mPhone,String code){
		Map<String, Object> resultMap=new HashMap<>();
		Member member=null;
		try {
			if(Common.isEmpty(mPhone)){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_LOGIN_NULL);
				return resultMap;
			}
			if(Common.isEmpty(code)){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_CAPTCHA_NOT_EXIST);
				return resultMap;
			}
			member=memberService.selectByPhone(mPhone);
			if(member==null){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_NOT_REGISTER);
				return resultMap;
			}else{
				Cache levelOneCache = factory.getCache("smsVerificationCode");
				ValueWrapper obj = levelOneCache.get(mPhone);
				//判断验证码是否存在，如不存在，则过期
				if(obj!=null){
					String codeExt = obj.get().toString();
					//校验当前手机号是否与缓存里是否一样，如不一样，则验证码错误
					if(codeExt!=null && codeExt.equals(code)){
						DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS,mPhone);
						return resultMap;
					}else{
						DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_CAPTCHA_ERROR);
						return resultMap;
					}
				}else{
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_CAPTCHA_OUTTIME);
					return resultMap;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_FAIL);
			return resultMap;
		}
	}
	/**跳转到输入密码页面*/
	@RequestMapping(value="/reset_psw")
	public String reset_psw(HttpServletRequest request,String mPhone){
		if(Common.isEmpty(mPhone)){
			String SID = request.getSession().getId();
			mPhone=((Member)(request.getSession().getAttribute(SessionContainer.sessionID + SID))).getmPhone();
		}
		request.setAttribute("mPhone", mPhone);
		return "/member/reset_psw";
	}
	/**跳转到密码找回完成页面*/
	@RequestMapping(value="/reset_done")
	public String reset_done(){
		return "/member/reset_done";
	}
	/**跳转到密码修改完成页面*/
	@RequestMapping(value="/update_done")
	public String update_done(){
		return "/member/update_done";
	}
}
